package com.woniuxy.cloud.carl.carlstation.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.cloud.carl.carlstation.client.StationClient;
import com.woniuxy.cloud.carl.carlstation.client.dto.StationDto;
import com.woniuxy.cloud.carl.carlstation.infra.mapper.StationMapper;
import com.woniuxy.cloud.carl.carlstation.infra.model.Station;
import com.woniuxy.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@CrossOrigin("*")
public class StationImpl implements StationClient {
    @Resource
    private StationMapper stationMapper;

    @Override
    public Result addStation(String stationName) {
        Station station = new Station();
        station.setStationName(stationName);
        stationMapper.insert(station);
        return Result.success();
    }

    @Override
    public Result<List<StationDto>> findAllStation() {
        List<Station> stations = stationMapper.selectList(null);
        List<StationDto> stationDtos = BeanUtil.copyToList(stations, StationDto.class);

        return Result.success(stationDtos);

    }
}
